package com.bank.gui;

import com.bank.utils.CommunicationWrapper;
import com.bank.utils.Operation;
import com.bank.utils.TextFieldLimiter;
import com.bank.utils.Toast;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.AbstractDocument;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Moofie
 */
public class WithdrawPanel extends javax.swing.JPanel {

    private boolean validate;
    private CommunicationWrapper cw;
    private String branchCode;
    private int portNo;

    /**
     * Creates new form WithdrawPanel
     */
    public WithdrawPanel(CommunicationWrapper cw, String branchCode, int portNo) {
        initComponents();
        this.cw = cw;
        this.branchCode = branchCode;
        this.portNo = portNo;
    }

    public static boolean isNumeric(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    /**
     * Reset all field to empty
     */
    private void reset() {
        jtfAccountNumber.setText("");
        jtfICNumber.setText("");
        jtfAmountWithdraw.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfAccountNumber = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfICNumber = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfAmountWithdraw = new javax.swing.JTextField();
        jbtSubmit = new javax.swing.JButton();
        jbtReset = new javax.swing.JButton();
        jlblAccBalance = new javax.swing.JLabel();
        jlblBalance = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setPreferredSize(new java.awt.Dimension(440, 380));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Account Number ");
        add(jLabel1);
        jLabel1.setBounds(20, 40, 390, 30);

        AbstractDocument aDoc = (AbstractDocument)jtfAccountNumber.getDocument();
        aDoc.setDocumentFilter(new TextFieldLimiter("\\d{0,14}"));
        jtfAccountNumber.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(jtfAccountNumber);
        jtfAccountNumber.setBounds(20, 70, 400, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("IC Number");
        add(jLabel2);
        jLabel2.setBounds(20, 110, 220, 30);

        AbstractDocument aDocIC = (AbstractDocument)jtfICNumber.getDocument();
        aDocIC.setDocumentFilter(new TextFieldLimiter("\\d{0,12}"));
        jtfICNumber.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(jtfICNumber);
        jtfICNumber.setBounds(20, 140, 400, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Amount to Withdraw");
        add(jLabel3);
        jLabel3.setBounds(20, 180, 180, 30);

        AbstractDocument aDocAmount = (AbstractDocument)jtfAmountWithdraw.getDocument();
        aDocAmount.setDocumentFilter(new TextFieldLimiter("\\d{0,5}"));
        jtfAmountWithdraw.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(jtfAmountWithdraw);
        jtfAmountWithdraw.setBounds(20, 210, 400, 30);

        jbtSubmit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jbtSubmit.setText("Submit");
        jbtSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmitActionPerformed(evt);
            }
        });
        add(jbtSubmit);
        jbtSubmit.setBounds(260, 320, 160, 50);

        jbtReset.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jbtReset.setText("Reset");
        jbtReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtResetActionPerformed(evt);
            }
        });
        add(jbtReset);
        jbtReset.setBounds(20, 320, 160, 50);

        jlblAccBalance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblAccBalance.setText("Account Balance                        : ");
        add(jlblAccBalance);
        jlblAccBalance.setBounds(20, 250, 210, 30);

        jlblBalance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(jlblBalance);
        jlblBalance.setBounds(230, 250, 180, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("MONEY WITHDRAWAL");
        add(jLabel5);
        jLabel5.setBounds(20, 5, 400, 20);
        add(jSeparator1);
        jSeparator1.setBounds(10, 25, 420, 5);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmitActionPerformed
        // prepare string for errors
        StringBuilder sb = new StringBuilder("<html>Errors");
        int height = 50;
        validate = true;

        if (jtfAccountNumber.getText().length() == 0) {
            validate = false;
            height += 10;
            sb.append("<br />Please enter account number");
        } else if (jtfAccountNumber.getText().length() > 0 && jtfAccountNumber.getText().length() != 14) {
            validate = false;
            height += 10;
            sb.append("<br />Please enter 12 digit account number");
        }

        if (jtfICNumber.getText().length() == 0) {
            height += 10;
            validate = false;
            sb.append("<br />Please enter ic number");
        }
        if (jtfAmountWithdraw.getText().length() == 0) {
            height += 10;
            validate = false;
            sb.append("<br />Please enter amount to withdraw");
        }
        if (!validate) {
            // validation failed, display error message
            sb.append("</html>");
            Toast.makeText(getParent(), height, "" + sb, Toast.LENGTH_LONG).display();
        } else {
            // validation passed, proceed to constructing data
            try {

                JSONObject j = new JSONObject();
                // specific operation
                j.put("operation", Operation.WITHDRAW);
                JSONObject content = new JSONObject();
                // put in necessary data
                content.put("accNo", jtfAccountNumber.getText());
                content.put("icNo", jtfICNumber.getText());
                content.put("amount", Double.parseDouble(jtfAmountWithdraw.getText()));
                content.put("port", this.portNo);
                content.put("bCode", this.branchCode);
                content.put("address", InetAddress.getLocalHost().getHostAddress());
                j.put("content", content);
                // after finish constructing, send to server
                cw.send(j, InetAddress.getLocalHost(), 5000);
                // wait for server's reply
                JSONObject js = cw.receive();
                JSONObject contents = js.getJSONObject("content");
                if(contents.get("result").toString().equalsIgnoreCase("Success")){
                    // operation succeed, display success message and returned value
                    Toast.makeText(getParent(), "Success", Toast.LENGTH_LONG).display();
                    jlblBalance.setText("RM " + String.format("%.2f", contents.getDouble("balance")));
                }else{
                    // operation failed, display error message
                    Toast.makeText(getParent(), "Please try again later", Toast.LENGTH_LONG).display();
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
                System.out.println(ex);
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
                Logger.getLogger(DepositPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }//GEN-LAST:event_jbtSubmitActionPerformed

    private void jbtResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtResetActionPerformed
        reset();
    }//GEN-LAST:event_jbtResetActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtReset;
    private javax.swing.JButton jbtSubmit;
    private javax.swing.JLabel jlblAccBalance;
    private javax.swing.JLabel jlblBalance;
    private javax.swing.JTextField jtfAccountNumber;
    private javax.swing.JTextField jtfAmountWithdraw;
    private javax.swing.JTextField jtfICNumber;
    // End of variables declaration//GEN-END:variables
}
